package com.seven.webhook.service.impl;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seven.webhook.model.bean.alertBean.AlertGroup;
import com.seven.webhook.model.bean.alertBean.RuleGroups;
import com.seven.webhook.config.MonitorServiceProperties;
import com.seven.webhook.model.MonitorGroup;
import com.seven.webhook.model.MonitorRule;
import com.seven.webhook.mapper.MonitorRuleMapper;
import com.seven.webhook.component.prometheus.PrometheusApiUtils;
import com.seven.webhook.service.MonitorGroupService;
import com.seven.webhook.service.MonitorRuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author seven
 * @since 2020-11-09
 */
@Service
public class MonitorRuleServiceImpl extends ServiceImpl<MonitorRuleMapper, MonitorRule> implements MonitorRuleService {

    @Autowired
    MonitorGroupService monitorGroupService;
    @Autowired
    MonitorServiceProperties monitorServiceProperties;

    @Override
    public boolean saveOrUpdate(MonitorRule entity) {
        //存表
        boolean b = super.saveOrUpdate(entity);
        //dump
        List<MonitorRule> list = list();
        // TODO: 11/11/2020 查询group
        MonitorGroup monitorGroup = new MonitorGroup();
        monitorGroup.setGroupName("hostStatsAlert");
        monitorGroup.setMonitorRules(list);
        RuleGroups ruleGroups = monitorGroup.parseRuleGroups();
        AlertGroup alertGroup = new AlertGroup();
        ArrayList<RuleGroups> ruleGroups1 = new ArrayList<>();
        ruleGroups1.add(ruleGroups);
        alertGroup.setGroups(ruleGroups1);
        alertGroup.dumpYamlFile(FileUtil.getWriter(monitorServiceProperties.getRulePath(), "UTF-8", false));
        //激活
        PrometheusApiUtils.activeRule();
        return b;
    }

    @Override
    public MonitorRule getByRuleName(String ruleName) {
        return getOne(new QueryWrapper<MonitorRule>().lambda().eq(MonitorRule::getRuleName, ruleName));
    }


    /*
    @Override
    public boolean saveOrUpdate(MonitorRule entity) {
        //入库
        boolean b = super.saveOrUpdate(entity);
        //更新yaml配置
        List<MonitorGroup> list = monitorGroupService.list();
        AlertGroup alertGroup = new AlertGroup();
        alertGroup.setGroups(MonitorGroup.parseRuleGroupList(list));
        alertGroup.dumpYamlFile(FileUtil.getWriter(
                new File(monitorServiceProperties.getRulePath()),
                "UTF-8", false));
        //激活
        PrometheusApiUtils.activeRule();
        return b;
    }

    @Override
    public boolean changeAlert(String id, boolean isAlert) {
        MonitorRule byId = getById(id);
        byId.setIsAlert(isAlert);
        if (isAlert) {
            if (StrUtil.isEmpty(byId.getSilenceId())) {
                //创建
                byId.setComment(byId.getComment() == null ? "" : byId.getComment());
                byId.setCreatedBy(byId.getCreatedBy() == null ? "auto" : byId.getCreatedBy());
                byId.setStartsAt(new Date());
                byId.setEndsAt(new Date(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(30)));
                Silence silence = byId.parseSilence();
                ArrayList<Matcher> matchers = new ArrayList<>();
                Matcher matcher = new Matcher();
                matcher.setName("alertname");
                matcher.setValue(byId.getRuleName());
                matchers.add(matcher);
                Matcher severity = new Matcher();
                severity.setName("severity");
                severity.setValue(byId.getSeverity());
                matchers.add(severity);
                silence.setMatchers(matchers);
                String silenceID = PrometheusApiUtils.silences(silence);
                byId.setSilenceId(silenceID);
            } else {
                PrometheusApiUtils.unSilences(byId.getSilenceId());
                byId.setSilenceId("");
            }
        } else {
            if (!StrUtil.isEmpty(byId.getSilenceId())) {
                Silence silences = PrometheusApiUtils.getSilences(byId.getSilenceId());
                silences.setId(null);
                String silenceID = PrometheusApiUtils.silences(silences);
                byId.setSilenceId(silenceID);
            }
        }
        boolean b = updateById(byId);
        return b;
    }
*/


}
